﻿namespace Modbus
{
	/// <summary>
	/// 
	/// </summary>
	internal static class FunctionCodes
	{
		/// <summary>
		/// 
		/// </summary>
		internal const byte ReadCoils = 0x01;

		/// <summary>
		/// 
		/// </summary>
		internal const byte ReadDiscreteInputs = 0x02;

		/// <summary>
		/// 
		/// </summary>
		internal const byte ReadHoldingRegisters = 0x03;

		/// <summary>
		/// 
		/// </summary>
		internal const byte ReadInputRegisters = 0x04;

		/// <summary>
		/// 
		/// </summary>
		internal const byte WriteSingleCoil = 0x05;

		/// <summary>
		/// 
		/// </summary>
		internal const byte WriteSingleRegister = 0x06;

		/// <summary>
		/// 
		/// </summary>
		internal const byte ReadExceptionStatus = 0x07;

		/// <summary>
		/// 
		/// </summary>
		internal const byte Diagnostics = 0x08;

		/// <summary>
		/// 
		/// </summary>
		internal const byte GetCommEventCounter = 0x0B;

		/// <summary>
		/// 
		/// </summary>
		internal const byte GetCommEventLog = 0x0C;

		/// <summary>
		/// 
		/// </summary>
		internal const byte WriteMultipleCoils = 0x0F;

		/// <summary>
		/// 
		/// </summary>
		internal const byte WriteMultipleRegisters = 0x10;

		/// <summary>
		/// 
		/// </summary>
		internal const byte MaskWriteRegister = 0x16;

		/// <summary>
		/// 
		/// </summary>
		internal const byte ReadWriteMultipleRegisters = 0x17;

		/// <summary>
		/// 
		/// </summary>
		internal const byte ReadFifoQueue = 0x18;

		/// <summary>
		/// 
		/// </summary>
		private const byte ExceptionBitMask = 0x80;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		internal static byte CleanException(byte value)
		{
			const byte codeMask = (byte)(ExceptionBitMask - 1);

			return (byte)(value & codeMask);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		internal static bool IsException(byte value)
		{
			return ((value & ExceptionBitMask) != 0);
		}
	}
}
